Skip to content

Compiler-As-1

Question 1

Let Σ={a,b}.Design a DFA for each following language (6 marks for each)

a.L1={abna|n2}

Current Stateab
q0q1q5
q1q5q2
q2q5q3
q3q4q3
q4q5q5
q5q5q5

1-a-DFA

b.L2={ban|n1 and n3}

Current Stateab
q0q6q1
q1q2q6
q2q3q6
q3q4q6
q4q5q6
q5q5q6
q6q6q6

1-b-DFA

c.L3={w||w|mod 30},where |w| is the length of the string w.

Current Stateab
q0q1q1
q1q2q2
q2q0q0

1-c-DFA

Question 2

Design a regular expression for each language in Q1. (6 marks for each)

a. abbba b. b(a|aa|aaaaa) c. ((a|b)(a|b)(a|b))((a|b)|((a|b)(a|b)))

Question 3

Let Σ={0,1}. Convert the regular expression (00)|(1(0|1)) to an NFA. (10 marks)

3-NFA

Question 4

Given the following NFA

4-NFA

a. Convert the NFA to the equivalent DFA. (20 marks)

First iteration

  • U0=ϵclosure(Q1)={Q1,Q2,Q5}
  • move(U0,0)={Q3,Q5}
  • ϵclosure(move(U0,0))={Q3,Q5}=U1
  • move(U0,1)={Q3,Q5,Q6}
  • ϵclosure(move(U0,1))={Q3,Q5,Q6}=U2

Second iteration

  • move(U1,0)={Q4}
  • ϵclosure(move(U1,0))={Q2,Q4,Q5}=U3
  • move(U1,1)={Q4,Q5,Q6}
  • ϵclosure(move(U1,1))={Q2,Q4,Q5,Q6}=U4

Third iteration

  • move(U2,0)={Q4,Q5,Q7}
  • ϵclosure(move(U2,0))={Q2,Q4,Q5,Q7}=U5
  • move(U2,1)={Q4,Q5,Q6}
  • ϵclosure(move(U2,1))={Q2,Q4,Q5,Q6}=U4

Fourth iteration

  • move(U3,0)={Q3,Q5}
  • ϵclosure(move(U3,0))={Q3,Q5}=U1
  • move(U3,1)={Q3,Q5,Q6}
  • ϵclosure(move(U3,1))=U2

Fifth iteration

  • move(U4,0)={Q3,Q5,Q7}
  • ϵclosure(move(U4,0))={Q3,Q5,Q7}=U6
  • move(U4,1)={Q3,Q5,Q6}
  • ϵclosure(move(U4,1))=U2

Sixth iteration

  • move(U5,0)={Q3,Q5}
  • ϵclosure(move(U5,0))=U1
  • move(U5,1)={Q3,Q5,Q6}
  • ϵclosure(move(U5,1))=U2

Seventh iteration

  • move(U6,0)={Q4,Q5}
  • ϵclosure(move(U5,0))={Q2,Q4,Q5}=U3
  • move(U6,1)={Q4,Q5,Q6}
  • ϵclosure(move(U5,1))={Q2,Q4,Q5,Q6}=U4
QQ01
{Q1,Q2,Q5}U0U1U2
{Q3,Q5}U1U3U4
{Q3,Q5,Q6}U2U5U4
{Q2,Q4,Q5}U3U1U2
{Q2,Q4,Q5,Q6}U4U6U2
{Q2,Q4,Q5,Q7}U5U1U2
{Q3,Q5,Q7}U6U3U4

4-a-DFA

b. Minimize the DFA in part a). (20 marks)

Initially, Q={{U1,U2,U5,U6},{U0,U3,U4}}

  • δ(U1,0)=U3, δ(U1,1)=U4
  • δ(U2,0)=U5, δ(U2,1)=U4
  • δ(U5,0)=U1, δ(U5,1)=U2
  • δ(U6,0)=U3, δ(U6,1)=U4

U1 and U6 act in the same way, Thus U1 and U6 should be in the same group.

L1={U1,U6} L2={U2} L3={U5}

QQ01
{Q1,Q2,Q5}U0U1U2
{Q3,Q5}U1U3U4
{Q3,Q5,Q6}U2U5U4
{Q2,Q4,Q5}U3U1U2
{Q2,Q4,Q5,Q6}U4U1U2
{Q2,Q4,Q5,Q7}U5U1U2
  • δ(U0,0)=U1, δ(U0,1)=U2
  • δ(U3,0)=U1, δ(U3,1)=U2
  • δ(U4,0)=U1, δ(U4,1)=U2

L4={U0,U3,U4}

U0 and U3 act in the same way, Thus U0 and U3 should be in the same group.

QQ01
{Q1,Q2,Q5}U0U1U2
{Q3,Q5}U1U0U0
{Q3,Q5,Q6}U2U5U0
{Q2,Q4,Q5,Q7}U5U1U2

Finally, the minimized DFA is:

L1={U1,U6}L2={U2}L3={U5}L4={U0,U3,U4}

4-b-DFA

Question 5

Consider a new alphabet Σ={a,,z}

a. Design a DFA with a trap state which recognizes the keywords if or else. (The DFA does not accept any other string.) (10 marks)

5-a-DFA

b. Represent the DFA using a transition table. (4 marks)

δifelsother
u0U1U2U3U2U2U2
u1U2U4U2U2U2U2
u2U2U2U2U2U2U2
u3U2U2U2U5U2U2
u4U2U2U2U2U2U2
u5U2U2U2U2U6U2
u6U2U2U7U2U2U2
u7U2U2U2U2U2U2